package day5;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author oldliu
 * @since 1.0
 */
public class MapTest5 {
    public static void main(String[] args) {
        Map m=new HashMap();
        m.put("cat","猫");
        m.put("dog","狗");
        m.put("tiger","老虎");
        m.put("goldenfish","金鱼");
        m.put("tiger","华南虎");
        m.put("table","桌子");
        m.put("desk","桌子");
        m.put("nothing",null);
        m.put(null,"zero");//?
        //所有的key和value
        //>>>System.out.println(m);
        //方法1：先迭代key,再找value
//        Set set = m.keySet();
//        //>>>Object[] objects = set.toArray();
//        Iterator ir = set.iterator();//对键 迭代
//        while (ir.hasNext()) {
//            String key = (String)ir.next();
//            String value=(String)m.get(key);
//            System.out.printf("key=%s,value=%s",key,value);
//        }
        //方法2：利用Map.entry
        Set set = m.entrySet();
        Iterator ir2 = set.iterator();
        while (ir2.hasNext()) {
            Map.Entry next = (Map.Entry) ir2.next();
            System.out.println("key="+next.getKey()+",value="+next.getValue());
        }
    }
}
